package com.huajin.iot.common.controller;

import com.huajin.iot.common.config.Constant;
import com.huajin.iot.common.domain.DictDO;
import com.huajin.iot.common.service.DictService;
import com.huajin.iot.common.utils.PageUtils;
import com.huajin.iot.common.utils.Query;
import com.huajin.iot.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.PastOrPresent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2017-09-29 18:28:07
 */
@Api(tags = {"数据字典管理接口"})
@RestController
@RequestMapping("/common/dict")
public class DictController extends BaseController {
    @Autowired
    private DictService dictService;


    @ApiOperation("数据字典列表")
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions("common:dict:dict")
    public R list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<DictDO> dictList = dictService.list(query);
        int total = dictService.count(query);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", dictList);
        resultMap.put("total", total);
        return R.ok(resultMap);
    }


    @ApiOperation("根据id查询数据字典")
    @GetMapping("/{id}")
    @RequiresPermissions("common:dict:edit")
    R getbyId(@PathVariable("id") Long id) {
        DictDO dict = dictService.get(id);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("dict", dict);
        return R.ok(resultMap);
    }

    /**
     * 保存
     */
    @ApiOperation("新增数据字典")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("common:dict:add")
    public R save(DictDO dict) {
        if (dictService.save(dict) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ApiOperation("编辑数据字典")
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("common:dict:edit")
    public R update(DictDO dict) {

        if (dictService.update(dict) > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 删除
     */
    @ApiOperation("删除数据字典")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("common:dict:remove")
    public R remove(Long id) {
        if (dictService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @ApiOperation("批量数据字典")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("common:dict:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        if (dictService.batchRemove(ids) == ids.length) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("数据字典类型列表")
    @GetMapping("/type")
    @ResponseBody
    public R listType() {
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", dictService.listType());
        return R.ok(resultMap);
    }

    @ApiOperation("根据类型下的数据字典")
    @ResponseBody
    @GetMapping("/list/{type}")
    public R listByType(@PathVariable("type") String type) {
        // 查询列表数据
        Map<String, Object> map = new HashMap<>(16);
        map.put("type", type);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", dictService.list(map));
        return R.ok(resultMap);
    }
}
